{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>05-for循环语句<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#for-循环进行数值循环\" data-toc-modified-id=\"for-循环进行数值循环-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>for 循环进行数值循环</a></span></li><li><span><a href=\"#for循环遍历列表和元组\" data-toc-modified-id=\"for循环遍历列表和元组-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>for循环遍历列表和元组</a></span></li><li><span><a href=\"#for-循环遍历字典\" data-toc-modified-id=\"for-循环遍历字典-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>for 循环遍历字典</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " for 循环，它常用于遍历字符串、列表、元组、字典、集合等序列类型，逐个获取序列中的各个元素。有限次数的循环常用for。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "for 循环的语法格式如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`\n",
    "for 迭代变量 in 字符串|列表|元组|字典|集合：\n",
    "    代码块\n",
    "`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for 循环进行数值循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在使用 for 循环时，最基本的应用就是进行数值循环。比如说，想要实现从 1 到 100 的累加："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算 1+2+...+100 的结果为：\n",
      "5050\n"
     ]
    }
   ],
   "source": [
    "print(\"计算 1+2+...+100 的结果为：\")\n",
    "\n",
    "result=0\n",
    "for i in range(101):\n",
    "    result+=i\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "range() 函数，此函数是 Python 内置函数，用于生成一系列连续整数(从0开始，左包含右不包含）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for循环遍历列表和元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用 for 循环遍历 list 列表或者 tuple 元组时，其迭代变量会先后被赋值为列表或元组中的每个元素并执行一次循环体。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表元素： 1\n",
      "列表元素： www\n",
      "列表元素： shell\n",
      "列表元素： 0.25\n"
     ]
    }
   ],
   "source": [
    "my_list = [1,'www','shell',0.25]\n",
    "\n",
    "for i in my_list:\n",
    "    print(\"列表元素：\",i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for 循环遍历字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 for 循环遍历字典时，经常会用到和字典相关的 3 个方法，即 items()、keys() 以及 values()。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "age\n",
      "sex\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'name':'lisan','age':18,'sex':'male'}\n",
    "for i in my_dict:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "直接遍历字典，默认是输出键。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name : lisan\n",
      "age : 18\n",
      "sex : male\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'name':'lisan','age':18,'sex':'male'}\n",
    "for key,value in my_dict.items():\n",
    "    print(key,\":\",value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过items()函数可以输出键值对。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "05-for循环语句",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
